package name.marz.to.go.ahg.mhbk.executer;

import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import name.marz.to.go.ahg.common.util.HttpUtil;
import name.marz.to.go.ahg.common.util.LogUtil;
import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.exception.InvalidDocumentException;
import name.marz.to.go.ahg.core.exception.UserProcessException;
import name.marz.to.go.ahg.core.executer.AbstractExecuter;
import name.marz.to.go.ahg.mhbk.exception.UserCancelException;
import name.marz.to.go.ahg.mhbk.gui.SecretQADialog;

import org.apache.http.client.ClientProtocolException;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class MHBKEntryClientSequenceExecuter extends AbstractExecuter<Document> {
	private static final Logger log = Logger.getLogger(MHBKEntryClientSequenceExecuter.class.getName());
	
	private Document firstDoc;

	public MHBKEntryClientSequenceExecuter(String uri,
			AutoGetterHttpClient client, Document doc) {
		super(uri, client);
		this.firstDoc = doc;
	}
	public MHBKEntryClientSequenceExecuter(URI baseuri,
			AutoGetterHttpClient client, Document doc) {
		super(baseuri, client);
		this.firstDoc = doc;
	}

	@Override
	public Document execute() throws ClientProtocolException, IOException,
			UserProcessException {
		LogUtil.log(Level.FINE, HttpUtil.getParametersFromInputTags(firstDoc), "1st entry sequence inputs");
		log.info("端末登録画面を起動します");
		// 合言葉１入力画面表示
		String q1 = findQuestion(firstDoc);
		SecretQADialog dialog1 = new SecretQADialog(q1,"合言葉１");
		SecretQADialog.Result result = dialog1.showDialog();
		// キャンセルなら 中断して例外をスロー
		if ( result == SecretQADialog.Result.Cancel ) throw new UserCancelException();
		// 合言葉1サーバー処理呼び出し
		URI uri = HttpUtil.createURISafety(super.uri.getScheme(),super.uri.getAuthority(),"/servlet/LOGWRD0010001B.do");
		MHBKSecretQAExecuter exec1 = new MHBKSecretQAExecuter(uri, client,firstDoc);
		exec1.setAnswer(dialog1.getAnswer());
		Document secondDoc = exec1.execute();
		// 合言葉２入力画面表示
		String q2 = findQuestion(secondDoc);
		SecretQADialog dialog2 = new SecretQADialog(q2,"合言葉2");
		result = dialog2.showDialog();
		// キャンセルなら 中断して例外をスロー
		if ( result == SecretQADialog.Result.Cancel ) throw new UserCancelException();
		// 合言葉２サーバー処理呼び出し
		MHBKSecretQAExecuter exec2 = new MHBKSecretQAExecuter(uri, client,secondDoc);
		exec2.setAnswer(dialog2.getAnswer());
		Document doc = exec2.execute();
		log.fine("QA2 response source="+doc.html());
		return doc;
	}
	private String findQuestion(Document doc) {
		// 質問の文字列を探す
		final String QUESTION_KEY1 = "question";
		Elements es = doc.getElementsByClass(QUESTION_KEY1);
		if ( es.isEmpty() ) throw new InvalidDocumentException("class "+QUESTION_KEY1+"が見つかりません", doc);
		
		final String QUESTION_KEY2 = "value";
		es = es.first().getElementsByClass(QUESTION_KEY2);
		if ( es.isEmpty() ) throw new InvalidDocumentException("class "+QUESTION_KEY2+"が見つかりません", doc);
		
		log.fine("質問 行 ソース="+es.first());
		return es.first().text();
	}
}
